<template>
  <div>
    <el-form :inline="true" :model="queryParam" class="demo-form-inline">
      <el-form-item label="查询条件">
        <el-input clearable size="mini" v-model="queryParam.keyword" placeholder="请输入部门名称或部门sn"></el-input>
      </el-form-item>
      <el-form-item label="部门状态">
        <el-select clearable size="mini" v-model="queryParam.state" placeholder="请选择部门状态">
          <el-option label="正常" value="0"></el-option>
          <el-option label="禁用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="toQuery">查询</el-button>
        <el-button type="success" size="mini" @click="onSubmit">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        prop="name"
        label="部门名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="sn"
        label="部门sn"
        width="180">
      </el-table-column>
      <el-table-column
        prop="state"
        label="部门状态">
        <template slot-scope="scop">
          <el-tag v-if="scop.row.state == 0">正常</el-tag>
          <el-tag v-else-if="scop.row.state == 1" type="success">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="managerId"
        label="部门管理员">
      </el-table-column>
      <el-table-column
        prop="parent.name"
        label="上级部门">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <!-- slot-scope="scope" 这是一个固定写法。当一列中，需要自定义样式，则可以通过slot-scope进行传值。当前这一行的数据，可以传入到 自定义的样式中
         scope.row ： 固定写法，作用：获取当前这一行的全部数据
         -->
        <template slot-scope="scope">
          <el-button @click="modify(scope.row)" type="text" size="small">修改</el-button>
          <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- elementui实现分页效果，必须接收一个结果：总记录数 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParam.currentPage"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="queryParam.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <el-dialog title="编辑部门信息" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="部门名称" label-width="80px" prop="name">
          <el-input clearable v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门sn" label-width="80px" prop="address">
          <el-input clearable v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门状态" label-width="80px">
          <el-input clearable v-model="form.date" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  var axios = require("axios")
  var mockjs = require('mockjs')
  export default {

    data() {
      return {
        queryParam: {
          currentPage:1,
          pageSize:1
        }, //所有的查询条件，都在当前这个对象中
        form: {},
        tableData: [],
        total:0,
        dialogFormVisible: false,
        rules: {
          name: [{
            required: true, message: "部门名称不能为空"
          }]
        }
      }
    },
    mounted() { //可以在页面加载完后，自动调用
        this.toQuery();
    },
    methods: {
      //这个方法是：每页条数发生变化时，触发的方法
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.queryParam.pageSize = val;
        this.toQuery();
      },
      //当前页码发生变化时，触发的方法
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.queryParam.currentPage = val;
        this.toQuery();
      },
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {

          } else {

          }
        })
      },
      toQuery() {
        // axios.post("/dept/findbyparams",this.queryParam).then(res => {
        //   // this.tableData = res.data;
        // })
        axios.post("/dept/findpagebyparams",this.queryParam).then(res => {
          // this.tableData = res.data;
          this.tableData = res.data.data;
          this.total = res.data.total;
        })

      },


      modify(row) {
        this.dialogFormVisible = true;
        this.form = row;
      },
      del(row) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //发送一个后台删除的请求即可
          alert(JSON.stringify(row))
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>
